//
//  YWRecordsManager.m
//  maintain
//
//  Created by Kyo on 16/9/24.
//  Copyright © 2016年 ewin. All rights reserved.
//

#import "YWRecordsManager.h"
#import "WMHttpTool.h"
#import "MJExtension.h"
#import "YWCommonTool.h"
#import "YWConst.h"
#import "YWMacro.h"

#import "YWMission.h"
#import "YWTroubleMissionModel.h"
#import "YWReportTroubleModel.h"
#import "YWPatrollineRecord.h"
#import "YWInspectionLineRecord.h"
#import "YWMeterReadingRecord.h"
#import "YWAttendanceRecord.h"
#import "YWEquipmentMaintenanceRecord.h"
#import "YWEquipmentTroubleRecord.h"
#import "YWKeepWatchDBManager.h"
#import "YWInspectionMissionDBManager.h"
#import "YWReportTroubleModel.h"

#import "YWBuilding.h"
#import "YWApartment.h"
#import "YWFloor.h"
#import "YWLocation.h"
#import "YWDBTool.h"

@implementation YWRecordsManager
YWSingletonM(Manager)

- (void)selectPostTroubleRecord:(NSInteger)page date:(NSString *)date buildingId:(NSString *)buildingId reporterId:(NSInteger)reporterId status:(NSInteger)status source:(NSInteger)source complete:(void (^)(NSArray *recordArray))complete{
    NSDictionary *headersDic = [YWCommonTool addSubHttpHeadWithDic:[WMHttpTool shareHttpTool].HTTPHeaders];
    [WMHttpTool shareHttpTool].HTTPHeaders = headersDic;
    if (headersDic == nil) {if (complete) {complete(nil);}return;}
    
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params setValue:@(page) forKey:@"page"];
    [params setValue:buildingId?:@"" forKey:@"buildingId"];
    if (date.length>0) {
        [params setValue:date forKey:@"reportDate"];
    }
    if (reporterId>0) {
        [params setValue:[@(reporterId) stringValue] forKey:@"reporterIds"];
    }
    if (status>=0) {
        [params setValue:[@(status) stringValue] forKey:@"troubleStatusArray"];
    }
    if (source>=0) {
        [params setValue:[@(source) stringValue] forKey:@"channels"];
    }
    
    YWLog(@"%@",params);
    [[WMHttpTool shareHttpTool] getWithUrl:[YWCommonTool reportRecordUrl] params:params success:^(NSHTTPURLResponse* response, id json) {
        NSArray* reportTroubleArray = [YWReportTroubleModel mj_objectArrayWithKeyValuesArray:json];
        
        [reportTroubleArray enumerateObjectsUsingBlock:^(YWReportTroubleModel* _Nonnull reportTrouble, NSUInteger idx, BOOL* _Nonnull stop) {
            YWBuilding* building = reportTrouble.building;
            YWApartment* apartment = reportTrouble.apartment;
            YWFloor* floor = reportTrouble.floor;
            YWLocation* location = reportTrouble.location;
            //保存 录入的报障的新建的位置
            if (building && apartment && floor && location && reportTrouble.equipment == nil && reportTrouble.channel == YWReportTroubleTypeEwinApp) {
                [[YWDBTool shareDBTool] updateIdAndNameWithBuilding:building apartment:apartment floor:floor location:location];
            }
        }];
        if (complete) {
            complete(reportTroubleArray);
        }
    } failure:^(NSHTTPURLResponse* response, NSError* error) {
        if (complete) {
            complete(nil);
        }
        if (![[WMHttpTool shareHttpTool] existenceNetwork]) {
            [YWCommonTool showTextHUDWithContent:NSLocalizedString(kTextNoNetwork, nil)];
        }else{
            [YWCommonTool showTextHUDWithContent:@"查询失败,请稍后再试!"];
        }
        YWLog(@"%s,error:%@", __func__, error.localizedDescription);
    }];
}

- (void)selectTroubleRecord:(NSInteger)page date:(NSString *)date buildingId:(NSString *)buildingId executorId:(NSInteger)executorId complete:(void (^)(NSArray *recordArray))complete{
    NSDictionary *headersDic = [YWCommonTool addSubHttpHeadWithDic:[WMHttpTool shareHttpTool].HTTPHeaders];
    [WMHttpTool shareHttpTool].HTTPHeaders = headersDic;
    if (headersDic == nil) {if (complete) {complete(nil);}return;}
    
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params setValue:@(page) forKey:@"page"];
    [params setValue:buildingId?:@"" forKey:@"buildingId"];
    if (date.length>0) {
        [params setValue:date forKey:@"date"];
    }
    if (executorId>0) {
        [params setValue:[@(executorId) stringValue] forKey:@"executorIds"];
    }
    YWLog(@"%@",params);
    [[WMHttpTool shareHttpTool] getWithUrl:[YWCommonTool repairRecordUrl] params:params success:^(NSHTTPURLResponse* response, id json) {
        NSArray* recordJsonArray = [YWEquipmentTroubleRecord mj_objectArrayWithKeyValuesArray:json];
        if (complete) {
            complete(recordJsonArray);
        }
    } failure:^(NSHTTPURLResponse* response, NSError* error) {
        if (complete) {
            complete(nil);
        }
        if (![[WMHttpTool shareHttpTool] existenceNetwork]) {
            [YWCommonTool showTextHUDWithContent:NSLocalizedString(kTextNoNetwork, nil)];
        }else{
            [YWCommonTool showTextHUDWithContent:@"查询失败,请稍后再试!"];
        }
        YWLog(@"%s,error:%@", __func__, error.localizedDescription);
    }];
}

- (void)selectKeepWatchRecord:(NSInteger)page date:(NSString *)date buildingId:(NSString *)buildingId executorId:(NSInteger)executorId status:(NSInteger)status lineId:(NSInteger)KeepWatchLineId complete:(void (^)(NSArray *recordArray))complete{
    NSDictionary *headersDic = [YWCommonTool addSubHttpHeadWithDic:[WMHttpTool shareHttpTool].HTTPHeaders];
    [WMHttpTool shareHttpTool].HTTPHeaders = headersDic;
    if (headersDic == nil) {if (complete) {complete(nil);}return;}
    
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params setValue:@(page) forKey:@"page"];
    [params setValue:buildingId?:@"" forKey:@"buildingId"];
    if (date.length>0) {
        [params setValue:date forKey:@"date"];
    }
    if (executorId>0) {
        [params setValue:[@(executorId) stringValue] forKey:@"executorIds"];
    }
    if (status>=0) {
        [params setValue:@(status) forKey:@"resultCodes"];
    }
    if (KeepWatchLineId>0) {
        [params setValue:[@(KeepWatchLineId) stringValue] forKey:@"patrolLineIds"];
    }
    
    YWLog(@"%@",params);
    [[WMHttpTool shareHttpTool] getWithUrl:[YWCommonTool keepWatchRecordUrl] params:params success:^(NSHTTPURLResponse* response, id json) {
        NSArray* lineRecordArray = [YWPatrollineRecord mj_objectArrayWithKeyValuesArray:json];
        [lineRecordArray enumerateObjectsUsingBlock:^(YWPatrollineRecord*  _Nonnull lineRecord, NSUInteger idx, BOOL * _Nonnull stop) {
            [[YWKeepWatchDBManager shareManager] updatePatrolLineWithLine:lineRecord.patrolLine];
        }];
        
        if (complete) {
            complete(lineRecordArray);
        }
    } failure:^(NSHTTPURLResponse* response, NSError* error) {
        if (complete) {
            complete(nil);
        }
        if (![[WMHttpTool shareHttpTool] existenceNetwork]) {
            [YWCommonTool showTextHUDWithContent:NSLocalizedString(kTextNoNetwork, nil)];
        }else{
            [YWCommonTool showTextHUDWithContent:@"查询失败,请稍后再试!"];
        }
        YWLog(@"%s,error:%@", __func__, error.localizedDescription);
    }];
}

- (void)selectInspectionRecord:(NSInteger)page date:(NSString *)date buildingId:(NSString *)buildingId executorId:(NSInteger)executorId  status:(NSInteger)status lineId:(NSInteger)inspectionLineId complete:(void (^)(NSArray *recordArray))complete{
    NSDictionary *headersDic = [YWCommonTool addSubHttpHeadWithDic:[WMHttpTool shareHttpTool].HTTPHeaders];
    [WMHttpTool shareHttpTool].HTTPHeaders = headersDic;
    if (headersDic == nil) {if (complete) {complete(nil);}return;}
    
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params setValue:@(page) forKey:@"page"];
    [params setValue:buildingId?:@"" forKey:@"buildingId"];
    if (date.length>0) {
        [params setValue:date forKey:@"date"];
    }
    if (executorId>0) {
        [params setValue:[@(executorId) stringValue] forKey:@"executorIds"];
    }
    if (status>=0) {
        [params setValue:[@(status) stringValue] forKey:@"resultCodes"];
    }
    if (inspectionLineId>0) {
        [params setValue:[@(inspectionLineId) stringValue] forKey:@"inspectionLineIds"];
    }
    YWLog(@"%@",params);
    [[WMHttpTool shareHttpTool] getWithUrl:[YWCommonTool inspectionRecordUrl] params:params success:^(NSHTTPURLResponse* response, id json) {
        NSArray* lineRecord = [YWInspectionLineRecord mj_objectArrayWithKeyValuesArray:json];
        [lineRecord enumerateObjectsUsingBlock:^(YWInspectionLineRecord*  _Nonnull lineRecord, NSUInteger idx, BOOL * _Nonnull stop) {
            [[YWInspectionMissionDBManager shareManager] updateInspectionLine:lineRecord.inspectionLine];
        }];
        if (complete) {
            complete(lineRecord);
        }
    } failure:^(NSHTTPURLResponse* response, NSError* error) {
        if (complete) {
            complete(nil);
        }
        if (![[WMHttpTool shareHttpTool] existenceNetwork]) {
            [YWCommonTool showTextHUDWithContent:NSLocalizedString(kTextNoNetwork, nil)];
        }else{
            [YWCommonTool showTextHUDWithContent:@"查询失败,请稍后再试!"];
        }
        YWLog(@"%s,error:%@", __func__, error.localizedDescription);
    }];
}

- (void)selectMeterReadingRecord:(NSInteger)page date:(NSString *)date buildingId:(NSString *)buildingId executorId:(NSInteger)executorId status:(NSInteger)status complete:(void (^)(NSArray *recordArray))complete{
    NSDictionary *headersDic = [YWCommonTool addSubHttpHeadWithDic:[WMHttpTool shareHttpTool].HTTPHeaders];
    [WMHttpTool shareHttpTool].HTTPHeaders = headersDic;
    if (headersDic == nil) {if (complete) {complete(nil);}return;}
    
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params setValue:@(page) forKey:@"page"];
    [params setValue:buildingId?:@"" forKey:@"buildingId"];
    if (date.length>0) {
        [params setValue:date forKey:@"date"];
    }
    if (executorId>0) {
        [params setValue:[@(executorId) stringValue] forKey:@"executorIds"];
    }
    if (status>=0) {
        if (status == 0) {
            [params setValue:@"Water_Meter" forKey:@"equipmentTypeOwnCode"];
        }else if (status==1){
            [params setValue:@"Electric_Meter" forKey:@"equipmentTypeOwnCode"];
        }else if (status==2){
            [params setValue:@"Gas_Meter" forKey:@"equipmentTypeOwnCode"];
        }
    }
    
    YWLog(@"%@",params);
    [[WMHttpTool shareHttpTool] getWithUrl:[YWCommonTool selectMeterReadingRecordUrl] params:params success:^(NSHTTPURLResponse* response, id json) {
        NSArray* readingRecord = [YWMeterReadingRecord mj_objectArrayWithKeyValuesArray:json];
        if (complete) {
            complete(readingRecord);
        }
    } failure:^(NSHTTPURLResponse* response, NSError* error) {
        if (complete) {
            complete(nil);
        }
        if (![[WMHttpTool shareHttpTool] existenceNetwork]) {
            [YWCommonTool showTextHUDWithContent:NSLocalizedString(kTextNoNetwork, nil)];
        }else{
            [YWCommonTool showTextHUDWithContent:@"查询失败,请稍后再试!"];
        }
        YWLog(@"%s,error:%@", __func__, error.localizedDescription);
    }];
}

- (void)selectDetectRecord:(NSInteger)page date:(NSString *)date buildingId:(NSString *)buildingId executorId:(NSInteger)executorId status:(NSInteger)status type:(NSInteger)type complete:(void (^)(NSArray *recordArray))complete{
    NSDictionary *headersDic = [YWCommonTool addSubHttpHeadWithDic:[WMHttpTool shareHttpTool].HTTPHeaders];
    [WMHttpTool shareHttpTool].HTTPHeaders = headersDic;
    if (headersDic == nil) {if (complete) {complete(nil);}return;}
    
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params setValue:@(page) forKey:@"page"];
    [params setValue:buildingId?:@"" forKey:@"buildingId"];
    if (date.length>0) {
        [params setValue:date forKey:@"date"];
    }
    if (executorId>0) {
        [params setValue:[@(executorId) stringValue] forKey:@"executorIds"];
    }
    if (status>=0) {
        [params setValue:[@(status) stringValue] forKey:@"statusList"];
    }
    
    YWLog(@"%@",params);
    [[WMHttpTool shareHttpTool] getWithUrl:[YWCommonTool detectionRecordUrl] params:params success:^(NSHTTPURLResponse* response, id json) {
        NSArray* missions = [YWMaintenanceMission mj_objectArrayWithKeyValuesArray:json];
        if (complete) {
            complete(missions);
        }
    } failure:^(NSHTTPURLResponse* response, NSError* error) {
        if (complete) {
            complete(nil);
        }
        if (![[WMHttpTool shareHttpTool] existenceNetwork]) {
            [YWCommonTool showTextHUDWithContent:NSLocalizedString(kTextNoNetwork, nil)];
        }else{
            [YWCommonTool showTextHUDWithContent:@"查询失败,请稍后再试!"];
        }
        YWLog(@"%s,error:%@", __func__, error.localizedDescription);
    }];
}

- (void)selectTempDetectRecord:(NSInteger)page date:(NSString *)date buildingId:(NSString *)buildingId executorId:(NSInteger)executorId status:(NSInteger)status type:(NSInteger)type complete:(void (^)(NSArray *))complete{
    NSDictionary *headersDic = [YWCommonTool addSubHttpHeadWithDic:[WMHttpTool shareHttpTool].HTTPHeaders];
    [WMHttpTool shareHttpTool].HTTPHeaders = headersDic;
    if (headersDic == nil) {if (complete) {complete(nil);}return;}
    
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params setValue:@(page) forKey:@"page"];
    [params setValue:buildingId?:@"" forKey:@"buildingId"];
    if (date.length>0) {
        [params setValue:date forKey:@"date"];
    }
    if (executorId>0) {
        [params setValue:[@(executorId) stringValue] forKey:@"executorIds"];
    }
    if (status>=0) {
         [params setValue:[@(status) stringValue] forKey:@"statusList"];
    }
   
    YWLog(@"%@",params);
    [[WMHttpTool shareHttpTool] getWithUrl:[YWCommonTool tempDetectionRecordUrl] params:params success:^(NSHTTPURLResponse* response, id json) {
        NSArray* record = [YWEquipmentMaintenanceRecord mj_objectArrayWithKeyValuesArray:json];
        if (complete) {
            complete(record);
        }
    } failure:^(NSHTTPURLResponse* response, NSError* error) {
        if (complete) {
            complete(nil);
        }
        if (![[WMHttpTool shareHttpTool] existenceNetwork]) {
            [YWCommonTool showTextHUDWithContent:NSLocalizedString(kTextNoNetwork, nil)];
        }else{
            [YWCommonTool showTextHUDWithContent:@"查询失败,请稍后再试!"];
        }
        YWLog(@"%s,error:%@", __func__, error.localizedDescription);
    }];
}

- (void)selectUpkeepRecord:(NSInteger)page date:(NSString *)date buildingId:(NSString *)buildingId executorId:(NSInteger)executorId status:(NSInteger)status type:(NSInteger)type complete:(void (^)(NSArray *recordArray))complete{
    NSDictionary *headersDic = [YWCommonTool addSubHttpHeadWithDic:[WMHttpTool shareHttpTool].HTTPHeaders];
    [WMHttpTool shareHttpTool].HTTPHeaders = headersDic;
    if (headersDic == nil) {if (complete) {complete(nil);}return;}
    
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params setValue:@(page) forKey:@"page"];
    [params setValue:buildingId?:@"" forKey:@"buildingId"];
    if (date.length>0) {
        [params setValue:date forKey:@"date"];
    }
    if (executorId>0) {
        [params setValue:[@(executorId) stringValue] forKey:@"executorIds"];
    }
    if (status>=0) {
        [params setValue:[@(status) stringValue] forKey:@"statusList"];
    }
    
    YWLog(@"%@",params);
    [[WMHttpTool shareHttpTool] getWithUrl:[YWCommonTool upkeepRecordUrl] params:params success:^(NSHTTPURLResponse* response, id json) {
        NSArray* missions = [YWMaintenanceMission mj_objectArrayWithKeyValuesArray:json];
        if (complete) {
            complete(missions);
        }
    } failure:^(NSHTTPURLResponse* response, NSError* error) {
        if (complete) {
            complete(nil);
        }
        if (![[WMHttpTool shareHttpTool] existenceNetwork]) {
            [YWCommonTool showTextHUDWithContent:NSLocalizedString(kTextNoNetwork, nil)];
        }else{
            [YWCommonTool showTextHUDWithContent:@"查询失败,请稍后再试!"];
        }
        YWLog(@"%s,error:%@", __func__, error.localizedDescription);
    }];
}

- (void)selectTempUpkeepRecord:(NSInteger)page date:(NSString *)date buildingId:(NSString *)buildingId executorId:(NSInteger)executorId status:(NSInteger)status type:(NSInteger)type complete:(void (^)(NSArray *))complete{
    NSDictionary *headersDic = [YWCommonTool addSubHttpHeadWithDic:[WMHttpTool shareHttpTool].HTTPHeaders];
    [WMHttpTool shareHttpTool].HTTPHeaders = headersDic;
    if (headersDic == nil) {if (complete) {complete(nil);}return;}
    
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params setValue:@(page) forKey:@"page"];
    [params setValue:buildingId?:@"" forKey:@"buildingId"];
    if (date.length>0) {
        [params setValue:date forKey:@"date"];
    }
    if (executorId>0) {
        [params setValue:[@(executorId) stringValue] forKey:@"executorIds"];
    }
    if (status>=0) {
        [params setValue:[@(status) stringValue] forKey:@"statusList"];
    }
    
    YWLog(@"%@",params);
    [[WMHttpTool shareHttpTool] getWithUrl:[YWCommonTool tempUpkeepRecordUrl] params:params success:^(NSHTTPURLResponse* response, id json) {
        NSArray* record = [YWEquipmentMaintenanceRecord mj_objectArrayWithKeyValuesArray:json];
        if (complete) {
            complete(record);
        }
    } failure:^(NSHTTPURLResponse* response, NSError* error) {
        if (complete) {
            complete(nil);
        }
        if (![[WMHttpTool shareHttpTool] existenceNetwork]) {
            [YWCommonTool showTextHUDWithContent:NSLocalizedString(kTextNoNetwork, nil)];
        }else{
            [YWCommonTool showTextHUDWithContent:@"查询失败,请稍后再试!"];
        }
        YWLog(@"%s,error:%@", __func__, error.localizedDescription);
    }];
}

- (void)selectAttendanceRecord:(NSInteger)page date:(NSString *)date buildingId:(NSString *)buildingId executorId:(NSInteger)executorId type:(NSInteger)type complete:(void (^)(NSArray *recordArray))complete{
    NSDictionary *headersDic = [YWCommonTool addSubHttpHeadWithDic:[WMHttpTool shareHttpTool].HTTPHeaders];
    [WMHttpTool shareHttpTool].HTTPHeaders = headersDic;
    if (headersDic == nil) {if (complete) {complete(nil);}return;}
    
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params setValue:@(page) forKey:@"page"];
    [params setValue:buildingId?:@"" forKey:@"buildingId"];
    if (date.length>0) {
        [params setValue:date forKey:@"date"];
    }
    if (executorId>0) {
        [params setValue:[@(executorId) stringValue] forKey:@"executorIds"];
    }
    if (type>=0) {
        [params setValue:[@(type) stringValue] forKey:@"types"];
    }
    
    YWLog(@"%@",params);
    [[WMHttpTool shareHttpTool] getWithUrl:[YWCommonTool selectAttendanceRecordUrl] params:params success:^(NSHTTPURLResponse* response, id json) {
        NSArray* record = [YWAttendanceRecord mj_objectArrayWithKeyValuesArray:json];
        if (complete) {
            complete(record);
        }
    } failure:^(NSHTTPURLResponse* response, NSError* error) {
        if (complete) {
            complete(nil);
        }
        if (![[WMHttpTool shareHttpTool] existenceNetwork]) {
            [YWCommonTool showTextHUDWithContent:NSLocalizedString(kTextNoNetwork, nil)];
        }else{
            [YWCommonTool showTextHUDWithContent:@"查询失败,请稍后再试!"];
        }
        YWLog(@"%s,error:%@", __func__, error.localizedDescription);
    }];
}
@end
